extern "C"
{
#include "utils.h"
}
#include "CppUTest/UtestMacros.h"
#include "CppUTest/TestHarness.h"

TEST_GROUP( UtilsTestGroup )
{
};

TEST( UtilsTestGroup, IsCodeFileTest )
{
    char filename[ 255 ] = "test.cpp";
    CHECK(iscodefile( filename ));

    strncpy( filename, "test.h", 255 );
    CHECK(iscodefile( filename ));

    strncpy( filename, "test.c", 255 );
    CHECK(iscodefile( filename ));

    strncpy( filename, "test.hpp", 255 );
    CHECK(iscodefile( filename ));

    strncpy( filename, "test.cc", 255 );
    CHECK(iscodefile( filename ));

    strncpy( filename, "test.json", 255 );
    CHECK_FALSE(iscodefile( filename ));

    strncpy( filename, "testjson", 255 );
    CHECK_FALSE(iscodefile( filename ));
}

TEST( UtilsTestGroup, IsWhitespaceTest )
{
    char chr;

    chr = ' ';
    CHECK(iswhitespace( chr ));

    chr = '\t';
    CHECK(iswhitespace( chr ));

    chr = '\t';
    CHECK(iswhitespace( chr ));

    chr = '\n';
    CHECK(iswhitespace( chr ));

    chr = 'a';
    CHECK_FALSE(iswhitespace( chr ));

}

TEST( UtilsTestGroup, IsOperatorTest )
{
    char chr;

    chr = '{';
    CHECK(isoperator( chr ));

    chr = ';';
    CHECK(isoperator( chr ));

    chr = ' ';
    CHECK_FALSE(isoperator( chr ));
}

TEST( UtilsTestGroup, IsEndWithBraceTest )
{
    char str[10];

    strncpy( str, "{hi}", 10 );
    CHECK(isendwithbrace( str ));

    strncpy( str, "if {", 10 );
    CHECK(isendwithbrace( str ));

    strncpy( str, "{", 10 );
    CHECK_FALSE(isendwithbrace( str ));

    /* did not distinguish this condition for brace */
    strncpy( str, "} else", 10 );
    CHECK_FALSE(isendwithbrace( str ));
}

TEST( UtilsTestGroup, StrRMExtraWSPTest )
{
    char str[20] = {0};
    char simplify_line[20] = {0};
    char expect_simplify_line[20] = {0};

    strncpy( str, "} else", 20 );
    strncpy( expect_simplify_line, "}else", 20 );
    str_rm_extra_wsp( str, simplify_line );
    STRCMP_EQUAL( expect_simplify_line, simplify_line);

    strncpy( str, "} else ", 20 );
    strncpy( expect_simplify_line, "}else", 20 );
    str_rm_extra_wsp( str, simplify_line );
    STRCMP_EQUAL( expect_simplify_line, simplify_line);

    strncpy( str, "a  + b == c", 20 );
    strncpy( expect_simplify_line, "a+b==c", 20 );
    str_rm_extra_wsp( str, simplify_line );
    STRCMP_EQUAL( expect_simplify_line, simplify_line);
}

TEST( UtilsTestGroup, IsCommentsTest )
{
    char str[20] = {0};
    bool multiple_line = false;

    strncpy( str, "//comment", 20 );
    multiple_line = false;
    CHECK( iscomments( str, &multiple_line ));
    CHECK_FALSE( multiple_line );

    strncpy( str, "/*comment*/", 20 );
    multiple_line = false;
    CHECK( iscomments( str, &multiple_line ));
    CHECK_FALSE( multiple_line );

    strncpy( str, "/*comments", 20 );
    multiple_line = false;
    CHECK( iscomments( str, &multiple_line ));
    CHECK( multiple_line );

    strncpy( str, "*comments", 20 );
    multiple_line = true;
    CHECK( iscomments( str, &multiple_line ));
    CHECK( multiple_line );

    strncpy( str, "*comments*/ ", 20 );
    multiple_line = true;
    CHECK( iscomments( str, &multiple_line ));
    CHECK_FALSE( multiple_line );

    strncpy( str, "comments ++; ", 20 );
    multiple_line = false;
    CHECK_FALSE( iscomments( str, &multiple_line ));
    CHECK_FALSE( multiple_line );
}
